<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Crond_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    //找出未处理订单
    public function all_find_orders()
    {
        $this->db->select('*');
        $this->db->from('shop_orders');
        $this->db->where('order_status',0);
        //$this->db->where('order_pay',3);
        $this->db->where('order_add_time<',time()-$this->config->item('jianlou_comfirm_wait'));       
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }        
    }
    //找到用户未处理订单
    public function all_find_orders_user_id($user_id)
    {
        $this->db->select('*');
        $this->db->from('shop_orders');
        $this->db->where('order_status',0);
        //$this->db->where('order_pay',3);
        $this->db->where('user_id',$user_id);
        $this->db->where('order_add_time<',time()-$this->config->item('jianlou_comfirm_wait'));       
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }   
    }
    //设为未处理,同时退款
    public function update_order_status($order_id)
    {
        $query = $this->db->select('*')->get_where('shop_orders', array('order_id'=>$order_id));
        $data = $query->row_array();
        $this->db->set('user_money','user_money+'.$data['order_price'],false)->where('user_id',$data['user_id'])->update('users');
        $money = array(
            "user_id"=>$data['user_id'],
            "money_num"=>$data['order_price'],
            "money_item"=>$data['good_id'],
            "money_type"=>1,
            "money_time"=>time(),
        );
        $this->db->insert('moneys',$money);
        $this->db->set('order_status',1,false)->set('order_update_time',time(),false)->where('order_id',$order_id)->update('shop_orders');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


}
